<?php
class M_terminal extends CI_Model {

  public function __construct() {
    // parent::__construct();
    $this->load->database();
  }
  /**
  *获取店铺详情 && 回访记录
  */
  public function getTerminallist($count,$offset){
    $sql = ' select * from terminal order by teid limit '.$offset.','.$count;
    $query = $this->db->query($sql);
    $total = $this->db->query('select * from terminal');
    return array(
      'terminallist' => $query->result_array(),
      'total'    => $total->num_rows(),
      'totallist'=> $total->result_array()
    );
  }
  /**
  *获取终端详情
  */
  public function getTerminalDetail($teid,$count,$offset){
    $sql = 'select * from terminal where teid='.$teid;
    $terminal =$this->db->query($sql);
    $callback = $this->db->query('select terminal_callback.id,teid,terminalcallback,addtime,emname as author from terminal_callback,employees where author = emid AND teid = '.$teid.' UNION ALL select terminal_callback.* FROM terminal_callback WHERE author = "admin" AND teid = '.$teid.' order by addtime desc limit '.$offset.','.$count);
    $totalcallback = $this->db->query('select * from terminal_callback where teid='.$teid);
    return array(
      'terminaldetail' => $terminal->result_array(),
      'flag'           => $terminal->num_rows(),
      'callback'   => $callback->result_array(),
      'total'      => $totalcallback->num_rows(),
      'totallist'  => $totalcallback->result_array()
    );
  }

  /**
  *添加终端
  */
  public function addTerminal($option){
    $sql = 'insert into terminal (shopid,bid,tename,tetel,tebirth,teteacher) values("'.$option['shopname'].'","'.$option['bname'].'","'.$option['tename'].'","'.$option['tetel'].'","'.$option['tebirth'].'","'.$option['teteacher'].'")';
    $this->db->simple_query($sql);
    $reid = $this->db->query('select max(teid) from terminal')->result_array();
    $shopid = explode(",", $option['shopid']);
    for ($i=0; $i < sizeof($shopid) ; $i++) {
      $this->db->simple_query('insert into teshop (teid,shopid) values('.$reid[0]['max(teid)'].','.$shopid[$i].')');
    }
    $bid = explode(",", $option['bid']);
    for ($i=0; $i < sizeof($bid) ; $i++) {
      $this->db->simple_query('insert into tebrand (teid,bid) values('.$reid[0]['max(teid)'].','.$bid[$i].')');
    }
    return '1';
    // return sizeof($shopid);
    // return $shopid;
    // return $this->db->simple_query($sql);
  }
  public function deleteTerminal($teid){
    if(!$teid)return false;
    $sql = 'delete from terminal where teid='.$teid;
    $this->db->simple_query('delete from teshop where teid='.$teid);
    $this->db->simple_query('delete from tebrand where teid='.$teid);
    $this->db->simple_query('delete from terminal_callback where teid='.$teid);
    return $this->db->simple_query($sql);
  }
  /**
  *添加回访
  */
  public function addTerminalCallback($teid,$val,$author){
    $sql = 'insert into terminal_callback(teid,terminalcallback,addtime,author) values('.$teid.',"'.$val.'","'.date("Y-m-d H:i:s").'","'.$author.'")';
    // return $sql;
    return $this->db->simple_query($sql);
  }
  /**
  *删除回访
  */
  public function deleteTerminalcallback($id){
    $sql = 'delete from terminal_callback where id='.$id;
    return $this->db->simple_query($sql);
  }
  /**
  *获取终端店铺
  */
  public function getteshop($teid){
    $sql = 'select teshop.shopid,shopname from teshop,shops where shops.shopid=teshop.shopid and teid='.$teid;
    return $this->db->query($sql)->result_array();
  }
  /**
  *获取终端品牌
  */
  public function gettebrand($teid){
    $sql = 'select tebrand.bid,bname from tebrand,brand where brand.bid=tebrand.bid and teid='.$teid;
    return $this->db->query($sql)->result_array();
  }
  /**
  *修改终端信息
  */
  public function updateTerminal($option){
    $sql = ' update terminal set tename="'.$option['tename'].
    '",tetel="'.$option['tetel'].
    '",tebirth="'.$option['tebirth'].
    '",bid="'.$option['bname'].
    '",shopid="'.$option['shopname'].
    '" where teid='.$option['teid'];
    $this->db->simple_query($sql);
    $this->db->simple_query('delete from teshop where teid='.$option['teid']);
    $shopid = explode(",", $option['shopid']);
    for ($i=0; $i < sizeof($shopid) ; $i++) {
      $this->db->simple_query('insert into teshop (teid,shopid) values('.$option['teid'].','.$shopid[$i].')');
    }
    $this->db->simple_query('delete from tebrand where teid='.$option['teid']);
    $bid = explode(",", $option['bid']);
    for ($i=0; $i < sizeof($bid) ; $i++) {
      $this->db->simple_query('insert into tebrand (teid,bid) values('.$option['teid'].','.$bid[$i].')');
    }
    return '1';
  }
  /**
  *修改回访
  */
  public function updateTerminalcallback($id,$val){
    $sql = 'update terminal_callback set terminalcallback="'.$val.'" where id='.$id;
    return $this->db->simple_query($sql);
  }
  /**
  *查看自己的回访记录
  */
  public function getMycallback($uid,$count,$offset){
    $callback = $this->db->query('select terminal_callback.*,tename from terminal_callback,terminal where terminal_callback.teid=terminal.teid and author='.$uid.' order by addtime desc limit '.$offset.','.$count);
    $totalcallback = $this->db->query('select terminal_callback.*,tename from terminal_callback,terminal where terminal_callback.teid=terminal.teid and author='.$uid);
    return array(
      'callback'   => $callback->result_array(),
      'total'      => $totalcallback->num_rows(),
      'totallist'  => $totalcallback->result_array()
    );
  }
  /**
  *查询回访记录
  */
  public function selectMycallback($selectkey,$uid,$count,$offset){
    // $callback = $this->db->query('select shop_callback.*,shopname from shop_callback,shops where shop_callback.shopid=shops.shopid and author='.$uid.' order by addtime desc limit '.$offset.','.$count);
    $sql = 'select terminal_callback.*,tename from terminal_callback,terminal ';
    // ' order by addtime desc limit '.$offset.','.$count
    if(!empty($selectkey)){
      $selectkey = '"%'.$selectkey.'%"';
      $sql = $sql." where ".
      '(tename like '.$selectkey.' or '.
      'terminalcallback like '.$selectkey.' ) '.
      'and terminal_callback.teid=terminal.teid and author='.$uid.
      ' order by addtime desc ';
      $total = $this->db->query($sql);
      $sql = $sql.'limit '.$offset.','.$count;
      $query = $this->db->query($sql);
      // return $sql;
      return array(
        'callback' => $query->result_array(),
        'total'    => $total->num_rows(),
        'totallist'=> $total->result_array()
      );
    }else{
      return false;
    }
  }
}
